﻿using Microsoft.AspNetCore.Mvc;
using Qy.Core;
using Qy.Scenario;
using Qy.Web.Authorize;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Qy.Web.Areas.Api;

[Route("Api/[controller]/[action]")]
[Verify]
public class UserGroupController : Base
{
    private readonly UserGroupService _userGroupService;
    private readonly MenuService _menuService;
    private readonly ApplicationContext _applicationContext;
    public UserGroupController(ApplicationContext applicationContext, UserGroupService userGroupService, MenuService menuService)
    {
        _userGroupService = userGroupService;
        _menuService = menuService;
        _applicationContext = applicationContext;
    }
    [HttpGet]
    [ActionPermissionFilter(Permission = PermissionKeys.UserGroup_Index)]
    public async Task<IActionResult> IndexAsync([FromQuery]QueryUserGroup query)
    {
        PagedInfo<UserGroup> list = await _userGroupService.GetPageListAsync(query);
        return Success(new { data = list.Result, count = list.TotalNum });
    }
    [HttpGet]
    [ActionPermissionFilter(Permission = PermissionKeys.UserGroup_AddOrEditView)]
    public async Task<IActionResult> AddOrEditAsync(int id)
    {
        IEnumerable<ViewMenu> menuList = await _menuService.GetCatTree();
        if (id < 1)
            return Success(new { menuList });
        UserGroup data = await _userGroupService.GetIdAsync(id);
        return Success(new { data = data, menuList });
    }

    [HttpPost, ActionName("Create")]
    [ModelStateFilter]
    [ActionPermissionFilter(Permission = PermissionKeys.UserGroup_AddOrEdit)]
    [Log(Name = "新增角色")]
    public async Task<IActionResult> CreatePostAsync([FromBody] UserGroup userGroup)
    {
        if (string.IsNullOrEmpty(userGroup.GroupName))
            return Fail("角色名不能为空！");
        if (await _userGroupService.GetByGroupNameAsync(userGroup.GroupName) == false)
            return Fail("该角色名已存在，请重新输入！");
        UserGroup res = await _userGroupService.InsertAndDelCacheAsync(userGroup);
        return Success($"添加角色:{res.GroupName}; ID：" + res.Id);
    }

    [HttpPut, ActionName("Edit")]
    [ModelStateFilter]
    [ActionPermissionFilter(Permission = PermissionKeys.UserGroup_AddOrEdit)]
    [Log(Name = "修改角色")]
    public async Task<IActionResult> EditPost([FromBody] UserGroup userGroup)
    {
        UserGroup get = await _userGroupService.GetIdAsync(userGroup.Id);
        if (get == null)
            return Fail("获取角色信息失败！");
        if (string.IsNullOrEmpty(userGroup.GroupName))
            return Fail("角色名不能为空！");
        if (userGroup.GroupName != get.GroupName)
            if (await _userGroupService.GetByGroupNameAsync(userGroup.GroupName) == false)
                return Fail("角色名已存在，请重新输入！");
        get.GroupName = userGroup.GroupName;
        get.SystemRole = userGroup.SystemRole;
        get.Remark = userGroup.Remark;
        await _userGroupService.UpdateAndDelCacheAsync(get);
        return Success($"修改角色及系统权限:{userGroup.GroupName} ID：{userGroup.Id}");
    }

    [HttpDelete]
    [ActionPermissionFilter(Permission = PermissionKeys.UserGroup_Delete)]
    [Log(Name = "删除角色")]
    public async Task<IActionResult> Delete(int id)
    {
        if (await _userGroupService.DeleteDelCache(id))
            return Success("删除角色 ID：" + id);
        else
            return Fail("删除失败！可能该角色下分配有用户");
    }

}
